package application.visual;

import io.IOClient;
import io.SerialCommunicator;

import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.util.Collection;

import javax.swing.JFrame;
import javax.swing.JPanel;

import api.Interpreter;

public class MainFrame extends JFrame{
	
	private MainPanel mainPanel;
	
	public MainFrame()
	{
		super();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		initGUI();
		setResizable(false);
		setTitle("USM Robotics");
	}
	
	public MainPanel getMainPanel()
	{
		if(mainPanel == null)
			mainPanel = new MainPanel();
		
		return mainPanel;
	}
	
	private void initGUI()
	{
		JPanel content = new JPanel();
		
		content.add(getMainPanel());
		
		getContentPane().add(content);
		
		pack();
	}
	
	public boolean variableCheck(String name)
	{
		return !getMainPanel().variableNameIsUsed(name);
	}
	public void addConnection(String variable, String commIdentifier, Interpreter i)
	{
		SerialCommunicator comm = SerialCommunicator.getCommunicator(commIdentifier);
		comm.connect();
		getMainPanel().addConnection(variable, comm, i);
	}
	
	public void removeConnection(IOClient cl)
	{
		getMainPanel().removeConnection(cl);
	}
	public Collection<IOClient> getConnectedClients()
	{
		return getMainPanel().getConnectedClients();
	}
	
	public void dispose()
	{
		for(IOClient c : getMainPanel().getConnectedClients())
			c.disconnect();
		
		super.dispose();
		
		System.exit(0);
	}
}
